package intialize

import (
	nsq2 "server/app/nsq"
	"server/base"
	"server/base/intialize/config"
	"server/pkg/cron"
	"server/pkg/mysql"
	"server/pkg/nsq"
	"server/pkg/redis"
	"server/pkg/websocket"
	"server/pkg/wx"
)

type Base struct{}

func (b Base) Init() {
	// 初始化配置文件
	config.Config{}.Init()

	// 初始化redis
	base.App.Redis = redis.InitRedis(
		base.App.Config.Redis.Host,
		base.App.Config.Redis.Port,
		base.App.Config.Redis.Password,
	)

	// 初始化mysql
	base.App.Mysql = mysql.InitMysql(
		base.App.Config.Mysql.Host,
		base.App.Config.Mysql.Port,
		base.App.Config.Mysql.Username,
		base.App.Config.Mysql.Password,
		base.App.Config.Mysql.Dbname,
		base.App.Config.Mysql.Log,
	)

	base.App.Mysql.Migrate()

	base.App.WxTools = wx.InitWx(base.App.Config.Wx.Appid, base.App.Config.Wx.AppSecret)

	base.App.WebSocketHub = websocket.NewHub()

	base.Mysql = base.App.Mysql.Object

	var err error
	base.NsqClient = nsq.NewNsqClient(base.App.Config.Nsqd.Host, base.App.Config.Nsqd.Port)
	if err != nil {
		panic(err)
	}

}

func (b Base) Run() {
	// 启动定时任务
	cron.Run()

	// 启动消息队列
	server := nsq.NsqServer{
		Host: base.App.Config.Nsqd.Host, Port: base.App.Config.Nsqd.Port,
		Retry: 5, Handel: nsq2.Register,
	}
	server.Run()
}
